<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>产品配置</el-breadcrumb-item>
        <el-breadcrumb-item>推送工具</el-breadcrumb-item>
        <el-breadcrumb-item>推送书籍</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        :table-loading="loading"
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :defaults.sync="defaults"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
        @row-del="rowDel"
        @row-save="rowSave"
        @row-update="rowUpdate"
      >
        <template slot-scope="scope" slot="last_operation_info">
          <div class="authorId">
            {{ scope.row.edit_time }}
            <el-tag>{{ scope.row.edit_admin_name }}</el-tag>
          </div>
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button
            type="text"
            icon="el-icon-back"
            v-if="scope.row.status == 1"
            @click="rowWithdraw(scope.row)"
            >撤回</el-button
          >
          <el-button
            type="text"
            icon="el-icon-edit"
            v-if="scope.row.status == 1"
            @click="rowEdit(scope.row)"
            >编辑</el-button
          >
          <el-button
            type="text"
            icon="el-icon-view"
            v-if="scope.row.status != 1"
            @click="rowView(scope.row)"
            >查看</el-button
          >
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import { pushTask as baseURL } from "@/api/cat/baseUrls";

export default {
  data() {
    return {
      page: {
        total: 0,
        pageSize: 10,
      },
      type: true,
      data: [],
      params: {},
      form: {},
      default: {
        date_type: 1,
      },
      option: {
        addBtn: true,
        delBtn: false,
        editBtn: false,
        index: true,
        indexLabel: "序号",
        border: true,
        column: [
          {
            label: "标题",
            prop: "title",
            type: "input",
            span: 24,
            rules: [
              {
                required: true,
                message: "标题必须填写",
              },
            ],
          },
          {
            label: "文案",
            prop: "content",
            type: "input",
            span: 24,
            hide: true,
            search: false,
          },
          {
            label: "书籍id",
            prop: "date",
            type: "input",
            span: 24,
            hide: true,
            search: false,
            rules: [
              {
                required: true,
                message: "书籍id必须填写",
              },
            ],
          },
          {
              label: '发送条件',
              span:24,
              prop: 'condition_array',
              type: 'checkbox',
              border:true,
              dicData:[{
                label:'安卓',
                value:'android'
              },{
                label:'iOS',
                value:'ios'
              },{
                label:'英语',
                value:'language-ens'
              },{
                label:'芬兰语',
                value:'language-fin'
              },{
                label:'男频',
                value:'male'
              },{
                label:'女频',
                value:'female'
              },{
                label:'付费',
                value:'pay'
              }]
          },
          {
            label: "发送日期",
            span: 24,
            prop: "send_time",
            type: "datetime",
            format: "yyyy-MM-dd HH:mm:ss",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            mock: {
              type: "datetime",
              format: "yyyy-MM-dd HH:mm:ss",
              now: true,
            },
            rules: [
              {
                required: true,
                message: "发送必须选择",
              },
            ],
          },
          {
            label: "最后操作信息",
            prop: "last_operation_info",
            type: "input",
            slot: true,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
            addDisabled: true,
            editDisabled: true,
          },
          {
            label: "状态",
            prop: "status",
            type: "select",
            span: 24,
            row: true,
            search: false,
            dicData: [
              {
                label: "预发送",
                value: 1,
              },
              {
                label: "发送中",
                value: 2,
              },
              {
                label: "发送成功",
                value: 3,
              },
              {
                label: "发送失败",
                value: 4,
              },
              {
                label: "已失效",
                value: 5,
              },
            ],
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true,
          },
          {
            label: "操作人",
            prop: "operator",
            type: "input",
            search: true,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
            addDisabled: true,
            editDisabled: true,
            hide: true,
          },
          {
            label: "日期查询",
            prop: "search_time_scope",
            type: "datetimerange",
            search: true,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
            addDisabled: true,
            editDisabled: true,
            hide: true,
          },
        ],
      },
    };
  },
  methods: {
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        baseURL.page,
        { ...this.default, ...this.params },
        this.page,
        "POST"
      );
      this.page = response.page;
      this.data = response.data;
      console.log(this.data);
      this.loading = false;
    },
    /**查看 */
    rowView(row) {
      this.$refs.crud.rowView(row, row.$index);
    },
    /**编辑 */
    rowEdit(row) {
      this.$refs.crud.rowEdit(row, row.$index);
    },
    /**撤回 */
    async rowWithdraw(row) {
      console.log(row);
      let userInfo = this.$store.state.user.userInfo;
      row.edit_admin_name = userInfo.username;
      row.edit_admin_id = userInfo.id;
      let data = await common(`${baseURL.withdraw}`, row, null, "POST");
      if (data == null) {
        return;
      }
      this.getList();
      done();
    },
    /**保存 */
    async rowSave(row, done, loading) {
      delete row["id"];
      let userInfo = this.$store.state.user.userInfo;
      row.date_type = 1;
      row.status = 1;
      row.condition_a = "all";
      row.edit_admin_name = userInfo.username;
      row.edit_admin_id = userInfo.id;
      let data = await common(`${baseURL.add}`, row, null, "POST");
      if (data == null) {
        return;
      }
      this.getList();
      done();
    },
    /**修改 */
    async rowUpdate(row, index, done, loading) {
      console.log(row);
      let userInfo = this.$store.state.user.userInfo;
      row.edit_admin_name = userInfo.username;
      row.edit_admin_id = userInfo.id;
      let data = await common(`${baseURL.update}`, row, null, "POST");
      if (data == null) {
        return;
      }
      this.getList();
      done();
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**搜索 */
    searchChange(params, done) {
      if (params.search_time_scope) {
        params.start_time = params.search_time_scope[0];
        params.end_time = params.search_time_scope[1];
      }
      this.params = params;
      this.page.currentPage = 1;
      console.log(params);
      this.getList();
      if (done) done();
    },
  },
  mounted() {},
};
</script>
<style lang="scss" scoped></style>
